﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using CarlosAg.ExcelXmlWriter;

namespace Medianamik.Globalization.Utilities
{
    public class ExcelResourceExporter : ResourceExporter<Workbook>
    {
        protected override Workbook CreateInstance()
        {
            return new Workbook();
        }
        protected override void AddResource(Workbook export, Resource resource)
        {
            Worksheet relatedWorksheet;
            if (!ContainsWorksheetWithSameName(export, resource.ResourceSet))
            {
                export.Worksheets.Add(resource.ResourceSet.Substring(0, resource.ResourceSet.Length < 31 ? resource.ResourceSet.Length : 31));
                relatedWorksheet = export.Worksheets[export.Worksheets.Count - 1];
            }
            else
            {
                relatedWorksheet = GetRelatedWorksheet(export, resource.ResourceSet);
            }
            

            if (relatedWorksheet.Table.Rows.Count == 0)
            {
                 AddFirstRowAsResourceSet(relatedWorksheet, resource.ResourceSet);
                 AddHeader(relatedWorksheet);
            }
            relatedWorksheet.Table.Rows.Add(resource.ToWorksheetRow());
        }
        private static Worksheet GetRelatedWorksheet(Workbook wb, string name)
        {
            foreach (Worksheet ws in wb.Worksheets)
            {
                if (ws.Table.Rows.Count > 0 &&
                    ws.Table.Rows[0].Cells.Count > 1 &&
                    ws.Table.Rows[0].Cells[1].Data.Text == name)
                    return ws; 
            }
            return null;
        }
        private static bool ContainsWorksheetWithSameName(Workbook wb, string worksheetName)
        {
            return GetRelatedWorksheet(wb, worksheetName) != null;
        }
        private static void AddHeader(Worksheet ws)
        {
            var row = new WorksheetRow();
            const DataType datatype = DataType.String;
            
            row.Cells.Add(new WorksheetCell("Resource ID", datatype));
            row.Cells.Add(new WorksheetCell("Localization", datatype));
            row.Cells.Add(new WorksheetCell("Value", datatype));

            ws.Table.Rows.Add(row);
        }
        private static void AddFirstRowAsResourceSet(Worksheet ws, string ResourceSet)
        {
            if (ws == null)
                throw new ArgumentException("ws");
            if(ws.Table.Rows.Count > 0)
                throw new InvalidOperationException("worksheet named " + ws.Name +
                                                    " already contains a row, cannot add ResourceSet");
            var row = new WorksheetRow();
            const DataType datatype = DataType.String;
            
            row.Cells.Add(new WorksheetCell("ResourceSet ID", datatype));
            row.Cells.Add(new WorksheetCell(ResourceSet, datatype));

            ws.Table.Rows.Add(row);
        }

        
    }

}
